import moment from 'moment';

// Format date to display
export const formatDate = (date: string | Date): string => {
  return moment(date).format('YYYY-MM-DD HH:mm:ss');
};

// Check if a seckill activity is active
export const isActivityActive = (startTime: string, endTime: string): boolean => {
  const now = moment();
  return now.isAfter(moment(startTime)) && now.isBefore(moment(endTime));
};

// Check if a seckill activity is upcoming
export const isActivityUpcoming = (startTime: string): boolean => {
  const now = moment();
  return now.isBefore(moment(startTime));
};

// Check if a seckill activity has ended
export const isActivityEnded = (endTime: string): boolean => {
  const now = moment();
  return now.isAfter(moment(endTime));
};

// Get remaining time for countdown
export const getRemainingTime = (targetTime: string): { days: number; hours: number; minutes: number; seconds: number } => {
  const now = moment();
  const target = moment(targetTime);
  const duration = moment.duration(target.diff(now));
  
  return {
    days: Math.max(0, Math.floor(duration.asDays())),
    hours: Math.max(0, duration.hours()),
    minutes: Math.max(0, duration.minutes()),
    seconds: Math.max(0, duration.seconds())
  };
};

// Format remaining time for display
export const formatRemainingTime = (time: { days: number; hours: number; minutes: number; seconds: number }): string => {
  const { days, hours, minutes, seconds } = time;
  if (days > 0) {
    return `${days}天 ${hours}小时 ${minutes}分钟`;
  } else if (hours > 0) {
    return `${hours}小时 ${minutes}分钟 ${seconds}秒`;
  } else if (minutes > 0) {
    return `${minutes}分钟 ${seconds}秒`;
  } else {
    return `${seconds}秒`;
  }
}; 